package com.itheima.chanlin.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.chanlin.common.R;
import com.itheima.chanlin.entity.Member;
import com.itheima.chanlin.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;
    /**
     * 新增员工
     * @param member
     * @return
     *
     */
    @PostMapping
    public R<String> save(HttpServletRequest request,
            @RequestBody Member member){
        log.info("新增员工信息：{}",member.toString());

//        member.setCreateTime(LocalDateTime.now());
//        member.setUpdateTime(LocalDateTime.now());

        //获得当前登录用户的id
//        Long empId = (Long) request.getSession().getAttribute("member");

//        member.setCreateUser(empId);
//        member.setUpdateUser(empId);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);
//         CountCard如果为null,就给他一个0
        if(member.getCountCard()==null){
            member.setCountCard(0);
        }
        if(member.getCardStartdate() == null || member.getCardStartdate()==""){
            member.setCardStartdate(format);
        }
        if(member.getCardStopdate() == null || member.getCardStopdate()==""){
            member.setCardStopdate(format);
        }
//       StartTime StopTime如果为空，就给他一个当前值
        if(member.getStartTime() == null  || member.getStartTime()==""){
            member.setStartTime(format);
        }
        if(member.getStopTime() == null || member.getStopTime()==""){
            member.setStopTime(format);
        }



        memberService.save(member);

        return R.success("新增员工成功");


    }

    /**
     * 员工信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        log.info("page = {},pageSize = {},name = {}" ,page,pageSize,name);

        //构造分页构造器
        Page pageInfo = new Page(page,pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Member> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Member::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Member::getUpdateTime);

        //执行查询
        memberService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }

    /**
     * 根据id修改员工信息
     * @param member
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,
            @RequestBody Member member){
        log.info(member.toString());
        /*保存修改人和修改时间*/
        Long empId = (Long)request.getSession().getAttribute("member");
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        String format = sdf.format(date);
//         CountCard如果为null,就给他一个0
//        if(member.getCountCard()==null){
//            member.setCountCard(0);
//        }
//        if(member.getCardStartdate() == null || member.getCardStartdate()==""){
//            member.setCardStartdate(format);
//        }
//        if(member.getCardStopdate() == null || member.getCardStopdate()==""){
//            member.setCardStopdate(format);
//        }
////       StartTime StopTime如果为空，就给他一个当前值
//        if(member.getStartTime() == null  || member.getStartTime()==""){
//            member.setStartTime(format);
//        }
//        if(member.getStopTime() == null || member.getStopTime()==""){
//            member.setStopTime(format);
//        }

        /*调用修改业务*/
        memberService.updateById(member);
        return R.success("员工信息修改成功");
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Member> getById(@PathVariable Long id){
        log.info("根据id查询员工信息...");
        Member member = memberService.getById(id);
        if(member != null){
            return R.success(member);
        }
        return R.error("没有查询到对应员工信息");
    }


    @DeleteMapping("/{id}")
    public R<String> deleteMember(@PathVariable Long id){

        log.info("删除会员，id为:{}",id);

        memberService.removeById(id);

//        memberService.remove(id);

        return R.success("会员信息删除成功");
    }

    @PostMapping("/phone")
    public R<Member> getByPhone(@RequestBody Member member){

        log.info("member为:{}",member);

        LambdaQueryWrapper<Member> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(member.getPhone()!=null,Member::getPhone,member.getPhone());

//        获取查询数据，如果没有查到数据给前台响应失败数据
        Member one = null;
        one = memberService.getOne(queryWrapper);
        if(one == null){
            return R.error("出现问题，请找管理员核验手机号...");
        }
        one = memberService.getOne(queryWrapper);
        one.setText("无权限");

        return R.success(one);
    }

    /**
     * 前台登录页面
     * @param member
     * @return
     */
    @PostMapping("/login")
    public R<String> login(@RequestBody Member member,
                            HttpSession session){

        log.info("member:{}",member);

        LambdaQueryWrapper<Member> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(member.getPhone()!=null,Member::getPhone,member.getPhone());
        queryWrapper.eq(member.getPass()!=null,Member::getPass,member.getPass());

//        获取查询数据，如果没有查到数据给前台响应失败数据
        Member one = null;
        one = memberService.getOne(queryWrapper);
        if(one == null){
            return R.error("手机号或者密码输入错误,请核验...");
        }
//        登录成功把数据放入session中
        session.setAttribute("phone",one.getPhone());

        return R.success("登录成功");
    }
}
